import { IResult } from "@/request/typeing";
import { getCurrentUrl } from "@/utils"

export interface IEnvData {
    api_path: string,
    appid: string,
    debug: "true" | "false",
    title: string,
    wx_pay_enable:"true" | "false",
    wx_pay_notify:string,
    mzzf:string
}

// 获取后台配置信息
export function getConfig() {

    // var config = {
    //     api_path: "/wx/v1/",
    //     appid: "wx0ef0964fda7b8fb0",
    //     debug: "true",
    //     title: "医院名称"
    // }
    // console.warn("服务器配置获取失败，已开启本地配置：", config);


    // return config


    const vite_base_url = import.meta.env.VITE_BASE_URL
    const base_url = vite_base_url ? vite_base_url : getCurrentUrl();
    const filePath = base_url as string + import.meta.env.VITE_GET_ENV_PATH + "get_env";

    let retObj = {}
    const xhr = new XMLHttpRequest();
    xhr.onload = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            const result = xhr.responseText

            if (result) {
                const res = JSON.parse(result) as IResult<IEnvData>

                retObj = res.data
            }
        }
    };

    // 发送同步请求
    xhr.open("get", filePath, false);
    xhr.send();
    
    
    return retObj
}